package views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import lib.Observer;
import lib.Subject;
import lib.persistence.ExcelToProject;

import java.awt.CardLayout;
import java.awt.Color;

import javax.swing.JSplitPane;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JToolBar;
import javax.swing.JToggleButton;
import javax.swing.JInternalFrame;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.FlowLayout;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JList;
/**
 * Example of components laid out in a grid
 */
public class Main extends javax.swing.JFrame implements Observer, ActionListener
{
	private static final long serialVersionUID = 1L;
	
	private JMenuBar jJMenuBar = null;

	private JMenu FileMenu = null;
	
	private JMenu HelpMenu = null;
	
	//Menu Items for 'File'
	
	private JMenuItem newMenuItem = null;
	private JMenuItem openMenuItem = null;
	private JMenuItem closeMenuItem = null;
	private JMenuItem saveMenuItem = null; 
	private JMenuItem exitMenuItem = null;
	
	//Menu Items for 'Upload'
	
	public javax.swing.JButton btQuit = null;
	private javax.swing.JPanel ivjJFrameContentPane = null;
	public JLabel lbProjectName = null;

	private JPanel mainPanel = null;

	public JButton btUploadFile = null;

	private JList liPeople = null;

	private JButton btOpenProject = null;

	private JLabel lbSelectProject = null;

	private JButton btCreateReport = null;

	public Main() {
		super();
		initialize();
		
		
		
	}

	/**
	 * Return the JButton1 property value.
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getJButton1() {
		if (btQuit == null) {
			btQuit = new javax.swing.JButton();
			btQuit.setName("btQuit");
			btQuit.setBounds(new Rectangle(332, 272, 75, 29));
			btQuit.setText("Quit");

		}
		
		return btQuit;
	}

	/**
	 * Return the JFrameContentPane property value.
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJFrameContentPane() {
		if (ivjJFrameContentPane == null) {
			lbSelectProject = new JLabel();
			lbSelectProject.setBounds(new Rectangle(13, 76, 114, 16));
			lbSelectProject.setText("Select Project:");
			lbProjectName = new JLabel();
			lbProjectName.setText("Welcome to PRS: The Project Reporting System");
			lbProjectName.setName("lbProjectName");
			ivjJFrameContentPane = new javax.swing.JPanel();
//			ivjJFrameContentPane.setBackground(Color.red);
			ivjJFrameContentPane.setLayout(null);
			ivjJFrameContentPane.setName("JFrameContentPane");
			ivjJFrameContentPane.setPreferredSize(new Dimension(500, 305));
			ivjJFrameContentPane.add(getMainPanel(), null);
			ivjJFrameContentPane.add(getJButton1(), null);
			ivjJFrameContentPane.add(getBtUploadFile(), null);
			ivjJFrameContentPane.add(getLiPeople(), null);
			ivjJFrameContentPane.add(getBtOpenProject(), null);
			ivjJFrameContentPane.add(lbSelectProject, null);
			ivjJFrameContentPane.add(getBtCreateReport(), null);
		}
		return ivjJFrameContentPane;
	}

	/**
	 * Initialize the class.
	 */
	private void initialize() {

		this.setName("JFrame1");
		this.setPreferredSize(new Dimension(855, 649));
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJFrameContentPane());
		this.setBounds(0, 0, 427, 356);
		this.setTitle("BasicSwingComponents");
		this.setJMenuBar(getJJMenuBar());
		this.setTitle("Project Reporting System");

	}

	//Create Menu bar
	
	private JMenuBar getJJMenuBar() 
	{
		if (jJMenuBar == null) 
		{
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}
	
	//Create menu drop down 'File'

	private JMenu getFileMenu() 
	{
		if (FileMenu == null) 
		{
			FileMenu = new JMenu();
			FileMenu.setText("File");
			FileMenu.add(getNewMenuItem());
			FileMenu.add(getOpenMenuItem());
			FileMenu.add(getCloseMenuItem());
			FileMenu.add(getSaveMenuItem());
			FileMenu.add(getExitMenuItem());
		}
		return FileMenu;
	}
	
	//Create menu drop down 'Report'
	
	private JMenu getHelpMenu() 
	{
		if (HelpMenu == null) 
		{
			HelpMenu = new JMenu();
			HelpMenu.setText("Help");
			//UploadMenu.add(getNewReserveMenuItem());
			//UploadMenu.add(getUpdateReserveMenuItem());
			//UploadMenu.add(getDeleteReserveMenuItem());
		}
		return HelpMenu;
	}
	
	//Create menu item 'File -- New'

	private JMenuItem getNewMenuItem() 
	{
		if (newMenuItem == null) 
		{
			newMenuItem = new JMenuItem();
			newMenuItem.setText("New");
			newMenuItem.addActionListener(this);
		}
		return newMenuItem;
	}
	
	//Create menu item 'File -- Open'
	
	private JMenuItem getOpenMenuItem() 
	{
		if (openMenuItem == null) 
		{
			openMenuItem = new JMenuItem();
			openMenuItem.setText("Open");
			openMenuItem.addActionListener(this);
		}
		return openMenuItem;
	}
	
	//Create menu item 'File -- Close'
	
	private JMenuItem getCloseMenuItem() 
	{
		if (closeMenuItem == null) 
		{
			closeMenuItem = new JMenuItem();
			closeMenuItem.setText("Close");
			closeMenuItem.addActionListener(this);
		}
		return closeMenuItem;
	}
	
	//Create menu item 'File -- Save' 
	
	private JMenuItem getSaveMenuItem() 
	{
		if (saveMenuItem == null) 
		{
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save");
			saveMenuItem.addActionListener(this);
		}
		return saveMenuItem;
	}
	
	//Create menu item 'File -- Exit'
	
	private JMenuItem getExitMenuItem() 
	{
		if (exitMenuItem == null) 
		{
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(this);
		}
		return exitMenuItem;
	}
	
	//Create menu item 'Upload -- Project'
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getActionCommand().equals("New"))
		{
			new ProjectView();
		}
		else if(e.getActionCommand().equals("Open"))
		{
			new Open();
		}
		else if(e.getActionCommand().equals("Close"))
		{
			System.exit(0);
		}
		else if(e.getActionCommand().equals("Save"))
		{
			//JFileChooser c = new JFileChooser();
			//new Open_Excel();
			
			JFileChooser c = new JFileChooser();
			int option = c.showOpenDialog(Main.this);
			
			if (option == JFileChooser.APPROVE_OPTION) 
			{
				String file_path = c.getSelectedFile().getAbsolutePath();
				
				
				String file_extension = file_path.substring(file_path.length() - 3, file_path.length());
				
				JOptionPane.showMessageDialog(null, file_extension);
				
				if (file_extension.compareToIgnoreCase("xls") != 0)
				{
					JOptionPane.showMessageDialog(null, "Sorry, you have selected a non XLS file. \n\n" + 
														"Please select a Excel file, version 97 to 2003.");	
				}
				
				else
				{
					new ExcelToProject(file_path);
				}
			}
					
			else 
			{
			    JOptionPane.showMessageDialog(null, "You canceled.");
			}
			
			
		} 
		else if(e.getActionCommand().equals("Project"))
		{
			System.exit(0);
		}
		else if(e.getActionCommand().equals("Person"))
		{
			new Person_Upload();
		}
		else if(e.getActionCommand().equals("Event"))
		{
			System.exit(0);
		}
		else if(e.getActionCommand().equals("Exit"))
		{
			System.exit(0);
		}
		
	}

	/**
	 * This method initializes mainPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getMainPanel() {
		if (mainPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.anchor = GridBagConstraints.CENTER;
			mainPanel = new JPanel();
			mainPanel.setLayout(new BoxLayout(getMainPanel(), BoxLayout.Y_AXIS));
//			mainPanel.setBackground(Color.blue);
			mainPanel.setBounds(new Rectangle(0, 0, 291, 16));
			mainPanel.add(lbProjectName, null);
		}
		return mainPanel;
	}

	@Override
	public void update(Subject o) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * This method initializes btUploadFile	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtUploadFile() {
		if (btUploadFile == null) {
			btUploadFile = new JButton();
			btUploadFile.setText("Upload File");
			btUploadFile.setBounds(new Rectangle(309, 27, 114, 29));
		}
		return btUploadFile;
	}

	/**
	 * This method initializes liPeople	
	 * 	
	 * @return javax.swing.JList	
	 */
	public JList getLiPeople() {
		if (liPeople == null) {
			liPeople = new JList();
			liPeople.setPreferredSize(new Dimension(200, 100));
			liPeople.setBounds(new Rectangle(7, 97, 416, 106));
		}
		return liPeople;
	}

	/**
	 * This method initializes btOpenProject	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getBtOpenProject() {
		if (btOpenProject == null) {
			btOpenProject = new JButton();
			btOpenProject.setBounds(new Rectangle(8, 208, 132, 29));
			btOpenProject.setText("Open Project");
		}
		return btOpenProject;
	}

	/**
	 * This method initializes btCreateReport	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getBtCreateReport() {
		if (btCreateReport == null) {
			btCreateReport = new JButton();
			btCreateReport.setBounds(new Rectangle(145, 209, 124, 29));
			btCreateReport.setText("Create Report");
		}
		return btCreateReport;
	}



}  //  @jve:decl-index=0:visual-constraint="10,10"
